#!/usr/bin/env node

const Router = require('koa-router'),
      log = console.log,
      users = require('../models/users'),
      jwt = require('jsonwebtoken'),
      {JWT_KEY} = require('../lib/constant.js'),
      _ = require('lodash'),
      router = new Router;
router.prefix('/api/users');


router.post('/login',async(ctx,next)=>{
  let {email,password} = ctx.request.body;
  let res =await users.login(email,password);
  if(res.code === 0){
    res.data = await jwt.sign(_.toPlainObject(res.data),JWT_KEY);
    
  }
  ctx.body = res;
})


module.exports = router;
